import {getUserInfo} from '@/api/userApi.js'
import { formatDate } from '../../utils/utils'

const state = {
	openId: '',
	nickname: '',
	headimgurl: '',
	unionId: '',
	score: 0,
	recentTime: ''
}
const getters = {
	userScore(state){
		const date = formatDate('yyyy-MM-dd')
		if(state.recentTime != date){
			return 0
		}
		console.log('userScore', state.score)
		return state.score
	},
	hasUserInfo: state=>{
		return state.nickname != ''
	}
}
const mutations = {
	setOpenid(state, openid){
		state.openId = openid
	},
	setUserInfo(state, user){
		state.nickname = user.nickname || ''
		state.headimgurl = user.headimgurl || user.avatarUrl || ''
		state.unionId = user.unionId || ''
	},
	setScore(state,payload){
		state.score = payload.score
		state.recentTime = payload.recentTime
	}
}

const actions = {
	fetchScore({state,commit}){
		console.log("fetchScore")
		getUserInfo().then((res)=>{
			commit('setScore', {score: res.score, recentTime: res.recentTime})
		}).catch((err)=>{})
	}
}

export default {
	state, getters, mutations, actions
}